package main;

import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

import entities.NPC;
import entities.Player;

import shared.BlockTypeList;
import shared.Camera;
import shared.Grid;
import shared.MouseInput;

public class GameLoop extends BasicGame {
	//Update Rate
	private int ticks = 60;
	private int maxTimePassed = (int)(1000.0/ticks);
	private int timePassed = 0;
	
	//Entities
	private Player player;
	
	public GameLoop() {
		super("The Fox");
	}

	@Override
	public void init(GameContainer gc) throws SlickException {
		BlockTypeList.init();
		
		//Entities
		player = new Player(100, 100, 40, 50);
		player.setTexture("resources/entities/player_sprite1.png");
		
		Grid.init(50);
		Map.init();
		
		Camera.init(player.getX(), player.getY(), player.getWidth(), player.getHeight());
		MouseInput.init();
		
		Map.generateRandomMap(-20, 20, 5, 30);
		
		Map.addNPC(new NPC(500, 0, 50, 50));

	}

	@Override
	public void update(GameContainer gc, int delta) throws SlickException {
		if(timePassed < maxTimePassed) {
			timePassed += delta;
		} else {
			timePassed = 0;
			
			//Input
			MouseInput.update(gc);
			
			//Player
			player.update(gc);
			
			//Map
			Map.update(player);
			
			//Grid
			Grid.update(gc, player);
			
			//Debug
			//System.out.println("MouseX: " + MouseInput.getMousePosX() + "; MouseY: " + MouseInput.getMousePosY());
		}
	}
	
	@Override
	public void render(GameContainer gc, Graphics g) throws SlickException {
		//Camera
		Camera.draw(g);
		
		//draw world
		g.setBackground(new Color(0, 100, 200));
		Map.draw(gc, g);
		
		//Player
		player.draw(gc, g);
		
		//draw mouse - always last
		Grid.drawMousePositionOnGrid(g);
	}

}
